/*
 * @Author: szx
 * @Date: 2022-01-20 14:47:04
 * @LastEditTime: 2022-01-20 14:59:15
 * @Description: 
 * @FilePath: \leetcode\100-199\121\121.js
 */
/**
 * @param {number[]} prices
 * @return {number}
 */
// dp[i][0] 表示第i天持有股票所得最多现金
// dp[i][1] 表示第i天不持有股票所得最多现金
var maxProfit = function (prices) {
    const n = prices.length;
    const dp = new Array(n).fill(0).map(() => new Array(2).fill(0));
    dp[0][0] = -prices[0];
    dp[0][1] = 0;
    for (let i = 1; i < n; i++) {
        dp[i][0] = Math.max(dp[i - 1][0], -prices[i])
        dp[i][1] = Math.max(dp[i - 1][1], prices[i] + dp[i - 1][0])
    }
    return dp[n - 1][1];
};